package com.netease.qa.biz.service.impl;

import com.nestease.qa.dao.api.ProjectDAO;
import com.nestease.qa.dao.api.TeamDAO;
import com.nestease.qa.dao.api.TestCaseDAO;
import com.nestease.qa.dao.api.UserTeamDAO;
import com.nestease.qa.dao.entity.Project;
import com.nestease.qa.dao.entity.Team;
import com.nestease.qa.dao.entity.TestCase;
import com.nestease.qa.dao.params.TestCaseQueryParams;
import com.netease.qa.biz.dto.TestCaseDTO;
import com.netease.qa.biz.enums.TestCaseStatus;
import com.netease.qa.biz.service.TestCaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/3/3
 */
@Service
public class TestCaseServiceImpl implements TestCaseService {

    @Resource
    private TestCaseDAO testCaseDAO;

    @Resource
    private TeamDAO teamDAO;

    @Resource
    private UserTeamDAO userTeamDAO;

    @Resource
    private ProjectDAO projectDAO;

    public int getNextTestCaseId() {
        int maxTestCaseId = testCaseDAO.getMaxTestCaseId();
        return ++maxTestCaseId;
    }

    public void addTestCase(TestCaseDTO testCaseDTO) {
        TestCase testCase = new TestCase();
        testCase.setCaseId(testCaseDTO.getCaseId());
        testCase.setTeamId(testCaseDTO.getTeamId());
        testCase.setProjectId(testCaseDTO.getProjectId());
        testCase.setRequestType(testCaseDTO.getRequestType());
        testCase.setUrl(testCaseDTO.getUrl());
        testCase.setHeaderKeys(testCaseDTO.getHeaderKeys());
        testCase.setHeaderValues(testCaseDTO.getHeaderValues());
        testCase.setParamKeys(testCaseDTO.getParamKeys());
        testCase.setParamValues(testCaseDTO.getParamValues());
        testCase.setParamRaw(testCaseDTO.getParamRaw());
        testCase.setJsonPathCheckPoint(testCaseDTO.getJsonPathCheckPoint());
        testCase.setCreator(testCaseDTO.getCreator());
        testCase.setLastEditor(testCaseDTO.getLastEditor());
        testCase.setStatus(TestCaseStatus.Valid.getValue());

        testCaseDAO.addTestCase(testCase);
    }

    public boolean ifTestCaseExist(int caseId) {
        TestCase testCase = testCaseDAO.getTestCaseDetail(caseId);
        return testCase != null ? true : false;
    }

    public List<TestCaseDTO> getTestCaseInCondition(TestCaseQueryParams params) {
        List<TestCase> testCaseList = testCaseDAO.getTestCaseInCondition(params);
        if (testCaseList != null) {
            List<TestCaseDTO> result = new ArrayList<TestCaseDTO>();

            for (TestCase testCase : testCaseList
                    ) {
                TestCaseDTO testCaseDTO = new TestCaseDTO();
                testCaseDTO.setCaseId(testCase.getCaseId());
                testCaseDTO.setUrl(testCase.getUrl());
                testCaseDTO.setStatusDesc(TestCaseStatus.getDesc(testCase.getStatus()));

                Team team = teamDAO.getTeamDetail(testCase.getTeamId());
                testCaseDTO.setTeamName(team.getTeamName());

                Project project = projectDAO.getProjectDetail(testCase.getProjectId());
                testCaseDTO.setProjectName(project.getProjectName());
                testCaseDTO.setEnv(project.getEnv());
                result.add(testCaseDTO);
            }
            return result;
        }
        return null;
    }

    public TestCaseDTO getTestCaseDetail(int caseId) {
        TestCase testCase = testCaseDAO.getTestCaseDetail(caseId);

        TestCaseDTO testCaseDTO = new TestCaseDTO();
        testCaseDTO.setCaseId(testCase.getCaseId());
        testCaseDTO.setUrl(testCase.getUrl());
        testCaseDTO.setStatus(testCase.getStatus());
        testCaseDTO.setStatusDesc(TestCaseStatus.getDesc(testCase.getStatus()));
        testCaseDTO.setHeaderKeys(testCase.getHeaderKeys());
        testCaseDTO.setHeaderValues(testCase.getHeaderValues());
        testCaseDTO.setParamKeys(testCase.getParamKeys());
        testCaseDTO.setParamValues(testCase.getParamValues());
        testCaseDTO.setParamRaw(testCase.getParamRaw());
        testCaseDTO.setJsonPathCheckPoint(testCase.getJsonPathCheckPoint());
        testCaseDTO.setRequestType(testCase.getRequestType());

        Team team = teamDAO.getTeamDetail(testCase.getTeamId());
        testCaseDTO.setTeamId(testCase.getTeamId());
        testCaseDTO.setTeamName(team.getTeamName());

        Project project = projectDAO.getProjectDetail(testCase.getProjectId());
        testCaseDTO.setProjectId(testCase.getProjectId());
        testCaseDTO.setProjectName(project.getProjectName());
        testCaseDTO.setEnv(project.getEnv());
        return testCaseDTO;
    }

    public void updateTestCase(TestCaseDTO testCaseDTO) {
        TestCase testCase = new TestCase();
        testCase.setCaseId(testCaseDTO.getCaseId());
        testCase.setTeamId(testCaseDTO.getTeamId());
        testCase.setProjectId(testCaseDTO.getProjectId());
        testCase.setRequestType(testCaseDTO.getRequestType());
        testCase.setUrl(testCaseDTO.getUrl());
        testCase.setHeaderKeys(testCaseDTO.getHeaderKeys());
        testCase.setHeaderValues(testCaseDTO.getHeaderValues());
        testCase.setParamKeys(testCaseDTO.getParamKeys());
        testCase.setParamValues(testCaseDTO.getParamValues());
        testCase.setParamRaw(testCaseDTO.getParamRaw());
        testCase.setJsonPathCheckPoint(testCaseDTO.getJsonPathCheckPoint());
        testCase.setLastEditor(testCaseDTO.getLastEditor());

        if (testCaseDTO.getRequestType().equals("GET")) {
            testCase.setParamRaw("");
        } else if (testCaseDTO.getRequestType().equals("POST")) {
            testCase.setParamKeys("");
            testCase.setParamValues("");
        }
        testCaseDAO.updateTestCase(testCase);
    }

    public void deleteTestCase(TestCaseDTO testCaseDTO) {
        TestCase testCase = new TestCase();
        testCase.setCaseId(testCaseDTO.getCaseId());
        testCase.setLastEditor(testCaseDTO.getLastEditor());

        testCaseDAO.deleteTestCase(testCase);
    }

    public boolean confirmUser(int caseId, String userId) {
        TestCase testCase = testCaseDAO.getTestCaseDetail(caseId);
        List<String> joinedUserIdList = userTeamDAO.getJoinedUserIdList(testCase.getTeamId());
        if (joinedUserIdList.contains(userId)) {
            return true;
        } else {
            return false;
        }
    }
}
